/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
    if (tab.url.indexOf('sde.maedhros.com') > -1) {
        // ... show the page action.
        chrome.pageAction.show(tabId);
    }
}

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

// when the page action button is clicked
chrome.pageAction.onClicked.addListener(function(tab) {
    chrome.tabs.create( {url: chrome.extension.getURL('scanview.html')} );
});



// listen to requests coming from the content scripts
chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        switch(request.action) {
            // login data was requested
            case "getLogin": {
                sendResponse(
                    {
                    'loginName': localStorage[request.module + "_login_name"],
                    'loginPass': localStorage[request.module + "_login_pass"]
                    }
                );
                break;
            }
            // send the last selected fightreport and some additional data
            case "getFightReport": {
                switch(request.page) {
                    case "warheart": {
                            sendResponse(
                                {
                                'report':       localStorage["storedKB"],
                                'showname':     localStorage["sde_warheart_nick"],
                                'autosubmit':   localStorage["sde_warheart_autosubmit"]
                                }
                            );
                            break;
                    }
                    case "coon": {
                            sendResponse(
                                {
                                'report':       localStorage["storedKB"],
                                'server':       localStorage["sde_coon_server"],
                                'shownames':    localStorage["sde_coon_nick"],
                                'autosubmit':   localStorage["sde_coon_autosubmit"]
                                }
                            );
                            break;
                    }
                }

                // delete KB after request, to make sure itt will not be pasted again when calling the sites alone
                localStorage['storedKB'] = undefined;
                break;
            }
            // store the recieved data into our localStorage
            case "storeData": {
                    localStorage[request.key] = request.value;
                    break;
            }
            case "storePlayerData": {
                    
                    break;
            }
            // store the recieved data into our localStorage
            case "retrieveData": {
                    sendResponse({
                        'value':     localStorage[request.key]
                    });
                    break;
            }
        }
    }
);